﻿using UnityEngine;
using System.Collections;

public abstract class OnScreenMessage : MonoBehaviour {

    public GUIStyle style;

    [SerializeField]
    protected float speed;
    [SerializeField]
    protected Vector3 range;
    [SerializeField]
    protected float lifeTime;

    public float Speed
    {
        get { return speed; }
        set { speed = value; }
    }

    public Vector3 Range
    {
        get { return range; }
        set { range = value; }
    }

    public float LifeTime
    {
        get { return lifeTime; }
        set { lifeTime = value; }
    }

    protected Vector3 screenPos;
    protected Vector3 goalPosition;
    private float timeSinceSpawn;
    bool stopMoving;

	// Use this for initialization
    protected virtual void Start()
    {
        Vector3 startPosition = Camera.main.WorldToScreenPoint(transform.position);
        startPosition.y = Screen.height - startPosition.y;
        goalPosition = startPosition + range;
        screenPos = startPosition;
        timeSinceSpawn = Time.time;
        stopMoving = false;
	}
	
	// Update is called once per frame
    protected virtual void Update()
    {
        if (stopMoving && (Time.time - timeSinceSpawn) > LifeTime)
        {
            Destroy(gameObject);
        }

        if (Vector2.Distance(screenPos, goalPosition) > 1)
        {
            screenPos = Vector2.Lerp(screenPos, goalPosition, Speed * Time.deltaTime);
        }
        else
        {
            stopMoving = true;
        }
	}

    void OnGUI()
    {
        printMessage();
    }

    protected abstract void printMessage();
}
